package com.tech.arno.spicy.ui.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;

import com.tech.arno.spicy.staticVal.Constant;

/*
*   package:     com.tech.arno.spicy.ui.fragment
*   fileName:    BaseFragment
*   author:    ArnoFrost
*   date:  2017/12/6 0006 16:50
*   desc:     自定义基础fragment
*/

public class BaseFragment extends Fragment {

    protected Activity mContext;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * @param code        自定义请求码
     * @param permissions 权限组
     * @name requestPermission
     * @function 申请指定的权限
     * @author ArnoFrost
     * @time 2017/12/19 0019  14:58
     */
    public void requestPermission(int code, String... permissions) {
        //只有在6.0及以上平台才开始动态申请权限
        if (Build.VERSION.SDK_INT >= 23) {
            requestPermissions(permissions, code);
        }
    }

    /**
     * @param permissions 权限组
     * @return boolean 是否已经授予权限
     * @name hasPermission
     * @function 判断是否有指定的权限
     * @author ArnoFrost
     * @time 2017/12/19 0019  14:57
     */
    public boolean hasPermission(String... permissions) {

        for (String permisson : permissions) {
            if (ContextCompat.checkSelfPermission(getActivity(), permisson)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case Constant.HARDWEAR_CAMERA_CODE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doOpenCamera();
                }
                break;
            case Constant.WRITE_READ_EXTERNAL_CODE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doWriteSDCard();
                }
            case Constant.READ_PHONE_CODE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doReadPhoneState();
                }
                break;
        }
    }

    //权限相关业务逻辑由具体子类来实现

    public void doWriteSDCard() {

    }

    public void doReadPhoneState() {
    }

    public void doOpenCamera() {

    }
}
